package com.yaoweigroup.community.adapter;

import java.util.List;

import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.bitmap.BitmapDisplayConfig;
import com.yaoweigroup.community.R;
import com.yaoweigroup.community.config.Urls;
import com.yaoweigroup.community.enity.Group;
import com.yaoweigroup.community.ui.home.GroupActivity;
import com.yaoweigroup.community.ui.home.GroupDetailActivity;
import com.yaoweigroup.community.util.BitmapHelp;
import com.yaoweigroup.community.util.Tools;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class GroupAdapter extends BaseAdapter {

	private Context context;
	private List<Group> list;
	private LayoutInflater mInflater;
	private BitmapUtils bitmapUtils;
	private BitmapDisplayConfig bigPicDisplayConfig;

	public GroupAdapter(Context context, List<Group> list) {
		mInflater = LayoutInflater.from(context);
		this.context = context;
		this.list = list;

		bitmapUtils = BitmapHelp.getBitmapUtils(context);
		bigPicDisplayConfig = new BitmapDisplayConfig();
		bigPicDisplayConfig.setLoadFailedDrawable(context.getResources()
				.getDrawable(R.drawable.bucket_no_picture));
		bigPicDisplayConfig.setLoadingDrawable(context.getResources()
				.getDrawable(R.drawable.bucket_no_picture));
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		Holder holder;
		if (convertView == null) {
			holder = new Holder();
			convertView = mInflater.inflate(R.layout.adapter_group_item, null);
			convertView.setTag(holder);

			holder.buy_btn = (Button) convertView.findViewById(R.id.buy_btn);
			holder.icon_iv = (ImageView) convertView.findViewById(R.id.icon_iv);
			holder.name_tv = (TextView) convertView.findViewById(R.id.name_tv);
			holder.discount_tv = (TextView) convertView
					.findViewById(R.id.discount_tv);
			holder.discount_num_tv = (TextView) convertView
					.findViewById(R.id.discount_num_tv);
			holder.buyNum_tv = (TextView) convertView
					.findViewById(R.id.buyNum_tv);
			holder.price_tv = (TextView) convertView
					.findViewById(R.id.price_tv);

		} else {
			holder = (Holder) convertView.getTag();
		}

		final Group group = list.get(position);
		holder.name_tv.setText(group.getTitle());
		bitmapUtils.display(holder.icon_iv, Urls.imgUrl + group.getImageurl(),
				bigPicDisplayConfig);
		holder.discount_tv.setText("现价:¥ " + group.getNewprice());
		holder.price_tv.setText("原价:¥" + group.getOldprice());
		holder.discount_num_tv.setText("折扣:"
				+ Tools.round((group.getNewprice() / group.getOldprice()) * 10,
						2) + "折");
		holder.buyNum_tv.setText("最近" + group.getSuccessNum() + "人成交");
		holder.buy_btn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(context, GroupDetailActivity.class);
				intent.putExtra("detail", group);
				context.startActivity(intent);
			}
		});
		return convertView;
	}

	public final class Holder {

		Button buy_btn;
		ImageView icon_iv;
		TextView name_tv;
		TextView discount_tv;
		TextView discount_num_tv;
		TextView buyNum_tv;
		TextView price_tv;
	}

	public void setDataChange(List<Group> list) {
		this.list = list;
		notifyDataSetChanged();
	}

}
